package com.twb.core.base.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.twb.core.base.Result;
import com.twb.core.constants.BaseEnums;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/")
@Api(tags = "错误页面跳转", value = "TwbErrorController")
public class TwbErrorController extends BaseController implements ErrorController {

	@Auth(verifyToken = false, verifyURL = false)
	@GetMapping("/error")
	@ApiOperation(value = "错误回执")
	public Result<String> searchBaseAppBanner(HttpServletRequest request) {
		HttpStatus status = getStatus(request);
		return Results.failureWithStatus(status.value(), BaseEnums.NOT_FOUND.desc());
	}

	private HttpStatus getStatus(HttpServletRequest request) {
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		if (statusCode != null) {
			try {
				return HttpStatus.valueOf(statusCode);
			} catch (Exception ex) {
			}
		}
		return HttpStatus.INTERNAL_SERVER_ERROR;
	}
}
